﻿/* 2013/1/10 */
using System;
using System.IO;

namespace Cosmos.Media.Play.IO
{
    /// <summary>
    /// Represents a writer that saves a play list to a M3U file.
    /// </summary>
    public class M3uWriter : IPlayListWriter, IDisposable
    {
        private const string _EXTM3U = "#EXTM3U";
        private const string _EXTINF = "#EXTINF:";

        private const string _TimeDetailKey = "Time";

        private FileStream _fileStream;
        private TextWriter _textWriter;
        private string _fileDirectory;

        /// <summary>
        /// Initializes a new instance of M3uWriter class with specified M3U file path.
        /// </summary>
        /// <param name="path">Path of the M3U file.</param>
        public M3uWriter(string path)
        {
            _fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
            _textWriter = new StreamWriter(_fileStream);

            _fileDirectory = Path.GetDirectoryName(path);
        }

        public void Close()
        {
            if (_textWriter != null)
            {
                _textWriter.Close();
            }

            if (_fileStream != null)
            {
                _fileStream.Close();
            }
        }

        public void Dispose()
        {
            if (_textWriter != null)
            {
                _textWriter.Dispose();
            }

            if (_fileStream != null)
            {
                _fileStream.Dispose();
            }
        }

        public void Flush()
        {
            if (_textWriter != null)
            {
                _textWriter.Flush();
            }
            if (_fileStream != null)
            {
                _fileStream.Flush();
            }
        }

        public void Save(PlayList playList)
        {
            int directoryPathLength = _fileDirectory.Length + 1;
            _textWriter.WriteLine(_EXTM3U);

            string infoString = null;
            string timeString = null;
            string musicPath = null;
            foreach (PlayListItem aMusicInfo in playList.Items)
            {
                /*if (!aMusicInfo.Details.TryGetValue(_TimeDetailKey, out timeString))
                {
                    timeString = "0";
                }*/

                timeString = "0";
                foreach (PlayAttribute anAttribute in aMusicInfo.Attributes)
                {
                    if (_TimeDetailKey.Equals(anAttribute.Name))
                    {
                        timeString = anAttribute.Value;
                    }
                }

                infoString = String.Concat(_EXTINF, timeString, ",", aMusicInfo.Name);
                _textWriter.WriteLine(infoString);

                musicPath = aMusicInfo.Path;
                if (musicPath.StartsWith(_fileDirectory))
                {
                    // If the music file is in the same directory with M3U file, save the relative path.
                    musicPath = musicPath.Substring(directoryPathLength);
                }
                _textWriter.WriteLine(musicPath);
            }
            musicPath = null;
            timeString = null;
            infoString = null;
        }
    }
}
